/sources/controllers/News.controller.php

https://github.com/Arantor/Elkarte · PHP · 1024 lines · 769 code · 106 blank · 149 comment · 186 complexity · 0396c4e954588e2f8c28f2ee34eb61bc MD5 · raw file

  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file contains the files necessary to display news as an XML feed.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Outputs xml data representing recent information or a profile.
  22. * Can be passed 4 subactions which decide what is output:
  23. * 'recent' for recent posts,
  24. * 'news' for news topics,
  25. * 'members' for recently registered members,
  26. * 'profile' for a member's profile.
  27. * To display a member's profile, a user id has to be given. (;u=1)
  28. * Outputs an rss feed instead of a proprietary one if the 'type' $_GET
  29. * parameter is 'rss' or 'rss2'.
  30. * Accessed via ?action=.xml.
  31. * Does not use any templates, sub templates, or template layers.
  32. *
  33. * @uses Stats language file.
  34. */
  35. function action_showfeed()
  36. {
  37. global $board, $board_info, $context, $scripturl, $boardurl, $txt, $modSettings, $user_info;
  38. global $query_this_board, $smcFunc, $forum_version, $cdata_override, $settings;
  39. // If it's not enabled, die.
  40. if (empty($modSettings['xmlnews_enable']))
  41. obExit(false);
  42. loadLanguage('Stats');
  43. // Default to latest 5. No more than whats defined in the ACP or 255
  44. $limit = empty($modSettings['xmlnews_limit']) ? 5 : min($modSettings['xmlnews_limit'], 255);
  45. $_GET['limit'] = empty($_GET['limit']) || (int) $_GET['limit'] < 1 ? $limit : min((int) $_GET['limit'], $limit);
  46. // Handle the cases where a board, boards, or category is asked for.
  47. $query_this_board = 1;
  48. $context['optimize_msg'] = array(
  49. 'highest' => 'm.id_msg <= b.id_last_msg',
  50. );
  51. if (!empty($_REQUEST['c']) && empty($board))
  52. {
  53. $_REQUEST['c'] = explode(',', $_REQUEST['c']);
  54. foreach ($_REQUEST['c'] as $i => $c)
  55. $_REQUEST['c'][$i] = (int) $c;
  56. if (count($_REQUEST['c']) == 1)
  57. {
  58. $request = $smcFunc['db_query']('', '
  59. SELECT name
  60. FROM {db_prefix}categories
  61. WHERE id_cat = {int:current_category}',
  62. array(
  63. 'current_category' => (int) $_REQUEST['c'][0],
  64. )
  65. );
  66. list ($feed_title) = $smcFunc['db_fetch_row']($request);
  67. $smcFunc['db_free_result']($request);
  68. $feed_title = ' - ' . strip_tags($feed_title);
  69. }
  70. $request = $smcFunc['db_query']('', '
  71. SELECT b.id_board, b.num_posts
  72. FROM {db_prefix}boards AS b
  73. WHERE b.id_cat IN ({array_int:current_category_list})
  74. AND {query_see_board}',
  75. array(
  76. 'current_category_list' => $_REQUEST['c'],
  77. )
  78. );
  79. $total_cat_posts = 0;
  80. $boards = array();
  81. while ($row = $smcFunc['db_fetch_assoc']($request))
  82. {
  83. $boards[] = $row['id_board'];
  84. $total_cat_posts += $row['num_posts'];
  85. }
  86. $smcFunc['db_free_result']($request);
  87. if (!empty($boards))
  88. $query_this_board = 'b.id_board IN (' . implode(', ', $boards) . ')';
  89. // Try to limit the number of messages we look through.
  90. if ($total_cat_posts > 100 && $total_cat_posts > $modSettings['totalMessages'] / 15)
  91. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 400 - $_GET['limit'] * 5);
  92. }
  93. elseif (!empty($_REQUEST['boards']))
  94. {
  95. $_REQUEST['boards'] = explode(',', $_REQUEST['boards']);
  96. foreach ($_REQUEST['boards'] as $i => $b)
  97. $_REQUEST['boards'][$i] = (int) $b;
  98. $request = $smcFunc['db_query']('', '
  99. SELECT b.id_board, b.num_posts, b.name
  100. FROM {db_prefix}boards AS b
  101. WHERE b.id_board IN ({array_int:board_list})
  102. AND {query_see_board}
  103. LIMIT ' . count($_REQUEST['boards']),
  104. array(
  105. 'board_list' => $_REQUEST['boards'],
  106. )
  107. );
  108. // Either the board specified doesn't exist or you have no access.
  109. $num_boards = $smcFunc['db_num_rows']($request);
  110. if ($num_boards == 0)
  111. fatal_lang_error('no_board');
  112. $total_posts = 0;
  113. $boards = array();
  114. while ($row = $smcFunc['db_fetch_assoc']($request))
  115. {
  116. if ($num_boards == 1)
  117. $feed_title = ' - ' . strip_tags($row['name']);
  118. $boards[] = $row['id_board'];
  119. $total_posts += $row['num_posts'];
  120. }
  121. $smcFunc['db_free_result']($request);
  122. if (!empty($boards))
  123. $query_this_board = 'b.id_board IN (' . implode(', ', $boards) . ')';
  124. // The more boards, the more we're going to look through...
  125. if ($total_posts > 100 && $total_posts > $modSettings['totalMessages'] / 12)
  126. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 500 - $_GET['limit'] * 5);
  127. }
  128. elseif (!empty($board))
  129. {
  130. $request = $smcFunc['db_query']('', '
  131. SELECT num_posts
  132. FROM {db_prefix}boards
  133. WHERE id_board = {int:current_board}
  134. LIMIT 1',
  135. array(
  136. 'current_board' => $board,
  137. )
  138. );
  139. list ($total_posts) = $smcFunc['db_fetch_row']($request);
  140. $smcFunc['db_free_result']($request);
  141. $feed_title = ' - ' . strip_tags($board_info['name']);
  142. $query_this_board = 'b.id_board = ' . $board;
  143. // Try to look through just a few messages, if at all possible.
  144. if ($total_posts > 80 && $total_posts > $modSettings['totalMessages'] / 10)
  145. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 600 - $_GET['limit'] * 5);
  146. }
  147. else
  148. {
  149. $query_this_board = '{query_see_board}' . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? '
  150. AND b.id_board != ' . $modSettings['recycle_board'] : '');
  151. $context['optimize_msg']['lowest'] = 'm.id_msg >= ' . max(0, $modSettings['maxMsgID'] - 100 - $_GET['limit'] * 5);
  152. }
  153. // Show in rss or proprietary format?
  154. $xml_format = isset($_GET['type']) && in_array($_GET['type'], array('smf', 'rss', 'rss2', 'atom', 'rdf', 'webslice')) ? $_GET['type'] : 'smf';
  155. // @todo Birthdays?
  156. // List all the different types of data they can pull.
  157. $subActions = array(
  158. 'recent' => array('action_xmlrecent', 'recent-post'),
  159. 'news' => array('action_xmlnews', 'article'),
  160. 'members' => array('action_xmlmembers', 'member'),
  161. 'profile' => array('action_xmlprofile', null),
  162. );
  163. // Easy adding of sub actions
  164. call_integration_hook('integrate_xmlfeeds', array($subActions));
  165. if (empty($_GET['sa']) || !isset($subActions[$_GET['sa']]))
  166. $_GET['sa'] = 'recent';
  167. // @todo Temp - webslices doesn't do everything yet.
  168. if ($xml_format == 'webslice' && $_GET['sa'] != 'recent')
  169. $xml_format = 'rss2';
  170. // If this is webslices we kinda cheat - we allow a template that we call direct for the HTML, and we override the CDATA.
  171. elseif ($xml_format == 'webslice')
  172. {
  173. $context['user'] += $user_info;
  174. $cdata_override = true;
  175. loadTemplate('Xml');
  176. }
  177. // We only want some information, not all of it.
  178. $cachekey = array($xml_format, $_GET['action'], $_GET['limit'], $_GET['sa']);
  179. foreach (array('board', 'boards', 'c') as $var)
  180. if (isset($_REQUEST[$var]))
  181. $cachekey[] = $_REQUEST[$var];
  182. $cachekey = md5(serialize($cachekey) . (!empty($query_this_board) ? $query_this_board : ''));
  183. $cache_t = microtime(true);
  184. // Get the associative array representing the xml.
  185. if (!empty($modSettings['cache_enable']) && (!$user_info['is_guest'] || $modSettings['cache_enable'] >= 3))
  186. $xml = cache_get_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, 240);
  187. if (empty($xml))
  188. {
  189. $xml = $subActions[$_GET['sa']][0]($xml_format);
  190. if (!empty($modSettings['cache_enable']) && (($user_info['is_guest'] && $modSettings['cache_enable'] >= 3)
  191. || (!$user_info['is_guest'] && (microtime(true) - $cache_t > 0.2))))
  192. cache_put_data('xmlfeed-' . $xml_format . ':' . ($user_info['is_guest'] ? '' : $user_info['id'] . '-') . $cachekey, $xml, 240);
  193. }
  194. $feed_title = htmlspecialchars(strip_tags($context['forum_name'])) . (isset($feed_title) ? $feed_title : '');
  195. // This is an xml file....
  196. ob_end_clean();
  197. if (!empty($modSettings['enableCompressedOutput']))
  198. @ob_start('ob_gzhandler');
  199. else
  200. ob_start();
  201. if ($xml_format == 'smf' || isset($_REQUEST['debug']))
  202. header('Content-Type: text/xml; charset=UTF-8');
  203. elseif ($xml_format == 'rss' || $xml_format == 'rss2' || $xml_format == 'webslice')
  204. header('Content-Type: application/rss+xml; charset=UTF-8');
  205. elseif ($xml_format == 'atom')
  206. header('Content-Type: application/atom+xml; charset=UTF-8');
  207. elseif ($xml_format == 'rdf')
  208. header('Content-Type: ' . (isBrowser('ie') ? 'text/xml' : 'application/rdf+xml') . '; charset=UTF-8');
  209. // First, output the xml header.
  210. echo '<?xml version="1.0" encoding="UTF-8"?' . '>';
  211. // Are we outputting an rss feed or one with more information?
  212. if ($xml_format == 'rss' || $xml_format == 'rss2')
  213. {
  214. // Start with an RSS 2.0 header.
  215. echo '
  216. <rss version=', $xml_format == 'rss2' ? '"2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"' : '"0.92"', ' xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">
  217. <channel>
  218. <title>', $feed_title, '</title>
  219. <link>', $scripturl, '</link>
  220. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>
  221. <generator>Elkarte</generator>
  222. <ttl>30</ttl>
  223. <image>
  224. <url>', $settings['default_theme_dir'], '/images/logo.png</url>
  225. <title>', $feed_title, '</title>
  226. <link>', $scripturl, '</link>
  227. </image>';
  228. // Output all of the associative array, start indenting with 2 tabs, and name everything "item".
  229. dumpTags($xml, 2, 'item', $xml_format);
  230. // Output the footer of the xml.
  231. echo '
  232. </channel>
  233. </rss>';
  234. }
  235. elseif ($xml_format == 'webslice')
  236. {
  237. $context['recent_posts_data'] = $xml;
  238. $context['can_pm_read'] = allowedTo('pm_read');
  239. // This always has RSS 2
  240. echo '
  241. <rss version="2.0" xmlns:mon="http://www.microsoft.com/schemas/rss/monitoring/2007" xml:lang="', strtr($txt['lang_locale'], '_', '-'), '">
  242. <channel>
  243. <title>', $feed_title, ' - ', $txt['recent_posts'], '</title>
  244. <link>', $scripturl, '?action=recent</link>
  245. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>
  246. <item>
  247. <title>', $feed_title, ' - ', $txt['recent_posts'], '</title>
  248. <link>', $scripturl, '?action=recent</link>
  249. <description><![CDATA[
  250. ', template_webslice_header_above(), '
  251. ', template_webslice_recent_posts(), '
  252. ', template_webslice_header_below(), '
  253. ]]></description>
  254. </item>
  255. </channel>
  256. </rss>';
  257. }
  258. elseif ($xml_format == 'atom')
  259. {
  260. foreach (array('board', 'boards', 'c') as $var)
  261. if (isset($_REQUEST[$var]))
  262. $url_parts[] = $var . '=' . (is_array($_REQUEST[$var]) ? implode(',', $_REQUEST[$var]) : $_REQUEST[$var]);
  263. echo '
  264. <feed xmlns="http://www.w3.org/2005/Atom">
  265. <title>', $feed_title, '</title>
  266. <link rel="alternate" type="text/html" href="', $scripturl, '" />
  267. <link rel="self" type="application/rss+xml" href="', $scripturl, '?type=atom;action=.xml', !empty($url_parts) ? ';' . implode(';', $url_parts) : '', '" />
  268. <id>', $scripturl, '</id>
  269. <icon>', $boardurl, '/favicon.ico</icon>
  270. <updated>', gmstrftime('%Y-%m-%dT%H:%M:%SZ'), '</updated>
  271. <subtitle><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></subtitle>
  272. <generator uri="http://www.elkarte.net" version="', strtr($forum_version, array('ELKARTE' => '')), '">ELKARTE</generator>
  273. <author>
  274. <name>', strip_tags($context['forum_name']), '</name>
  275. </author>';
  276. dumpTags($xml, 2, 'entry', $xml_format);
  277. echo '
  278. </feed>';
  279. }
  280. //@todo to not change much, rdf by default, maybe better use rss?
  281. else //if ($xml_format == 'rdf')
  282. {
  283. echo '
  284. <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/">
  285. <channel rdf:about="', $scripturl, '">
  286. <title>', $feed_title, '</title>
  287. <link>', $scripturl, '</link>
  288. <description><![CDATA[', strip_tags($txt['xml_rss_desc']), ']]></description>
  289. <items>
  290. <rdf:Seq>';
  291. foreach ($xml as $item)
  292. echo '
  293. <rdf:li rdf:resource="', $item['link'], '" />';
  294. echo '
  295. </rdf:Seq>
  296. </items>
  297. </channel>
  298. ';
  299. dumpTags($xml, 1, 'item', $xml_format);
  300. echo '
  301. </rdf:RDF>';
  302. }
  303. obExit(false);
  304. }
  305. /**
  306. * Called from dumpTags to convert data to xml
  307. * Finds urls for local sitte and santizes them
  308. *
  309. * @param type $val
  310. * @return type
  311. */
  312. function fix_possible_url($val)
  313. {
  314. global $modSettings, $context, $scripturl;
  315. if (substr($val, 0, strlen($scripturl)) != $scripturl)
  316. return $val;
  317. call_integration_hook('integrate_fix_url', array(&$val));
  318. if (empty($modSettings['queryless_urls']) || ($context['server']['is_cgi'] && ini_get('cgi.fix_pathinfo') == 0 && @get_cfg_var('cgi.fix_pathinfo') == 0) || (!$context['server']['is_apache'] && !$context['server']['is_lighttpd']))
  319. return $val;
  320. $val = preg_replace('/^' . preg_quote($scripturl, '/') . '\?((?:board|topic)=[^#"]+)(#[^"]*)?$/e', '\'\' . $scripturl . \'/\' . strtr(\'$1\', \'&;=\', \'//,\') . \'.html$2\'', $val);
  321. return $val;
  322. }
  323. /**
  324. * Ensures supplied data is properly encpsulated in cdata xml tags
  325. * Called from action_xmlprofile in News.controller.php
  326. *
  327. * @param type $data
  328. * @param type $ns
  329. * @return type
  330. */
  331. function cdata_parse($data, $ns = '')
  332. {
  333. global $smcFunc, $cdata_override;
  334. // Are we not doing it?
  335. if (!empty($cdata_override))
  336. return $data;
  337. $cdata = '<![CDATA[';
  338. for ($pos = 0, $n = $smcFunc['strlen']($data); $pos < $n; null)
  339. {
  340. $positions = array(
  341. $smcFunc['strpos']($data, '&', $pos),
  342. $smcFunc['strpos']($data, ']', $pos),
  343. );
  344. if ($ns != '')
  345. $positions[] = $smcFunc['strpos']($data, '<', $pos);
  346. foreach ($positions as $k => $dummy)
  347. {
  348. if ($dummy === false)
  349. unset($positions[$k]);
  350. }
  351. $old = $pos;
  352. $pos = empty($positions) ? $n : min($positions);
  353. if ($pos - $old > 0)
  354. $cdata .= $smcFunc['substr']($data, $old, $pos - $old);
  355. if ($pos >= $n)
  356. break;
  357. if ($smcFunc['substr']($data, $pos, 1) == '<')
  358. {
  359. $pos2 = $smcFunc['strpos']($data, '>', $pos);
  360. if ($pos2 === false)
  361. $pos2 = $n;
  362. if ($smcFunc['substr']($data, $pos + 1, 1) == '/')
  363. $cdata .= ']]></' . $ns . ':' . $smcFunc['substr']($data, $pos + 2, $pos2 - $pos - 1) . '<![CDATA[';
  364. else
  365. $cdata .= ']]><' . $ns . ':' . $smcFunc['substr']($data, $pos + 1, $pos2 - $pos) . '<![CDATA[';
  366. $pos = $pos2 + 1;
  367. }
  368. elseif ($smcFunc['substr']($data, $pos, 1) == ']')
  369. {
  370. $cdata .= ']]>&#093;<![CDATA[';
  371. $pos++;
  372. }
  373. elseif ($smcFunc['substr']($data, $pos, 1) == '&')
  374. {
  375. $pos2 = $smcFunc['strpos']($data, ';', $pos);
  376. if ($pos2 === false)
  377. $pos2 = $n;
  378. $ent = $smcFunc['substr']($data, $pos + 1, $pos2 - $pos - 1);
  379. if ($smcFunc['substr']($data, $pos + 1, 1) == '#')
  380. $cdata .= ']]>' . $smcFunc['substr']($data, $pos, $pos2 - $pos + 1) . '<![CDATA[';
  381. elseif (in_array($ent, array('amp', 'lt', 'gt', 'quot')))
  382. $cdata .= ']]>' . $smcFunc['substr']($data, $pos, $pos2 - $pos + 1) . '<![CDATA[';
  383. $pos = $pos2 + 1;
  384. }
  385. }
  386. $cdata .= ']]>';
  387. return strtr($cdata, array('<![CDATA[]]>' => ''));
  388. }
  389. /**
  390. * Formats data retrieved in other functions into xml format.
  391. * Additionally formats data based on the specific format passed.
  392. * This function is recursively called to handle sub arrays of data.
  393. * @param array $data the array to output as xml data
  394. * @param int $i the amount of indentation to use.
  395. * @param string $tag if specified, it will be used instead of the keys of data.
  396. * @param string $xml_format
  397. */
  398. function dumpTags($data, $i, $tag = null, $xml_format = '')
  399. {
  400. global $modSettings, $context, $scripturl;
  401. // For every array in the data...
  402. foreach ($data as $key => $val)
  403. {
  404. // Skip it, it's been set to null.
  405. if ($val === null)
  406. continue;
  407. // If a tag was passed, use it instead of the key.
  408. $key = isset($tag) ? $tag : $key;
  409. // First let's indent!
  410. echo "\n", str_repeat("\t", $i);
  411. // Grr, I hate kludges... almost worth doing it properly, here, but not quite.
  412. if ($xml_format == 'atom' && $key == 'link')
  413. {
  414. echo '<link rel="alternate" type="text/html" href="', fix_possible_url($val), '" />';
  415. continue;
  416. }
  417. // If it's empty/0/nothing simply output an empty tag.
  418. if ($val == '')
  419. echo '<', $key, ' />';
  420. elseif ($xml_format == 'atom' && $key == 'category')
  421. echo '<', $key, ' term="', $val, '" />';
  422. else
  423. {
  424. // Beginning tag.
  425. if ($xml_format == 'rdf' && $key == 'item' && isset($val['link']))
  426. {
  427. echo '<', $key, ' rdf:about="', fix_possible_url($val['link']), '">';
  428. echo "\n", str_repeat("\t", $i + 1);
  429. echo '<dc:format>text/html</dc:format>';
  430. }
  431. elseif ($xml_format == 'atom' && $key == 'summary')
  432. echo '<', $key, ' type="html">';
  433. else
  434. echo '<', $key, '>';
  435. if (is_array($val))
  436. {
  437. // An array. Dump it, and then indent the tag.
  438. dumpTags($val, $i + 1, null, $xml_format);
  439. echo "\n", str_repeat("\t", $i), '</', $key, '>';
  440. }
  441. // A string with returns in it.... show this as a multiline element.
  442. elseif (strpos($val, "\n") !== false || strpos($val, '<br />') !== false)
  443. echo "\n", fix_possible_url($val), "\n", str_repeat("\t", $i), '</', $key, '>';
  444. // A simple string.
  445. else
  446. echo fix_possible_url($val), '</', $key, '>';
  447. }
  448. }
  449. }
  450. /**
  451. * Retrieve the list of members from database.
  452. * The array will be generated to match the format.
  453. * @todo get the list of members from subs/Members.subs.php.
  454. *
  455. * @param string $xml_format
  456. * @return array
  457. */
  458. function action_xmlmembers($xml_format)
  459. {
  460. global $scripturl, $smcFunc;
  461. if (!allowedTo('view_mlist'))
  462. return array();
  463. // Find the most recent members.
  464. $request = $smcFunc['db_query']('', '
  465. SELECT id_member, member_name, real_name, date_registered, last_login
  466. FROM {db_prefix}members
  467. ORDER BY id_member DESC
  468. LIMIT {int:limit}',
  469. array(
  470. 'limit' => $_GET['limit'],
  471. )
  472. );
  473. $data = array();
  474. while ($row = $smcFunc['db_fetch_assoc']($request))
  475. {
  476. // Make the data look rss-ish.
  477. if ($xml_format == 'rss' || $xml_format == 'rss2')
  478. $data[] = array(
  479. 'title' => cdata_parse($row['real_name']),
  480. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  481. 'comments' => $scripturl . '?action=pm;sa=send;u=' . $row['id_member'],
  482. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['date_registered']),
  483. 'guid' => $scripturl . '?action=profile;u=' . $row['id_member'],
  484. );
  485. elseif ($xml_format == 'rdf')
  486. $data[] = array(
  487. 'title' => cdata_parse($row['real_name']),
  488. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  489. );
  490. elseif ($xml_format == 'atom')
  491. $data[] = array(
  492. 'title' => cdata_parse($row['real_name']),
  493. 'link' => $scripturl . '?action=profile;u=' . $row['id_member'],
  494. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['date_registered']),
  495. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['last_login']),
  496. 'id' => $scripturl . '?action=profile;u=' . $row['id_member'],
  497. );
  498. // More logical format for the data, but harder to apply.
  499. else
  500. $data[] = array(
  501. 'name' => cdata_parse($row['real_name']),
  502. 'time' => htmlspecialchars(strip_tags(timeformat($row['date_registered']))),
  503. 'id' => $row['id_member'],
  504. 'link' => $scripturl . '?action=profile;u=' . $row['id_member']
  505. );
  506. }
  507. $smcFunc['db_free_result']($request);
  508. return $data;
  509. }
  510. /**
  511. * Get the latest topics information from a specific board,
  512. * to display later.
  513. * The returned array will be generated to match the xmf_format.
  514. * @todo does not belong here
  515. *
  516. * @param $xml_format
  517. * @return array, array of topics
  518. */
  519. function action_xmlnews($xml_format)
  520. {
  521. global $user_info, $scripturl, $modSettings, $board;
  522. global $query_this_board, $smcFunc, $settings, $context;
  523. /* Find the latest posts that:
  524. - are the first post in their topic.
  525. - are on an any board OR in a specified board.
  526. - can be seen by this user.
  527. - are actually the latest posts. */
  528. $done = false;
  529. $loops = 0;
  530. while (!$done)
  531. {
  532. $optimize_msg = implode(' AND ', $context['optimize_msg']);
  533. $request = $smcFunc['db_query']('', '
  534. SELECT
  535. m.smileys_enabled, m.poster_time, m.id_msg, m.subject, m.body, m.modified_time,
  536. m.icon, t.id_topic, t.id_board, t.num_replies,
  537. b.name AS bname,
  538. mem.hide_email, IFNULL(mem.id_member, 0) AS id_member,
  539. IFNULL(mem.email_address, m.poster_email) AS poster_email,
  540. IFNULL(mem.real_name, m.poster_name) AS poster_name
  541. FROM {db_prefix}topics AS t
  542. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)
  543. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  544. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  545. WHERE ' . $query_this_board . (empty($optimize_msg) ? '' : '
  546. AND {raw:optimize_msg}') . (empty($board) ? '' : '
  547. AND t.id_board = {int:current_board}') . ($modSettings['postmod_active'] ? '
  548. AND t.approved = {int:is_approved}' : '') . '
  549. ORDER BY t.id_first_msg DESC
  550. LIMIT {int:limit}',
  551. array(
  552. 'current_board' => $board,
  553. 'is_approved' => 1,
  554. 'limit' => $_GET['limit'],
  555. 'optimize_msg' => $optimize_msg,
  556. )
  557. );
  558. // If we don't have $_GET['limit'] results, try again with an unoptimized version covering all rows.
  559. if ($loops < 2 && $smcFunc['db_num_rows']($request) < $_GET['limit'])
  560. {
  561. $smcFunc['db_free_result']($request);
  562. if (empty($_REQUEST['boards']) && empty($board))
  563. unset($context['optimize_msg']['lowest']);
  564. else
  565. $context['optimize_msg']['lowest'] = 'm.id_msg >= t.id_first_msg';
  566. $context['optimize_msg']['highest'] = 'm.id_msg <= t.id_last_msg';
  567. $loops++;
  568. }
  569. else
  570. $done = true;
  571. }
  572. $data = array();
  573. while ($row = $smcFunc['db_fetch_assoc']($request))
  574. {
  575. // Limit the length of the message, if the option is set.
  576. if (!empty($modSettings['xmlnews_maxlen']) && $smcFunc['strlen'](str_replace('<br />', "\n", $row['body'])) > $modSettings['xmlnews_maxlen'])
  577. $row['body'] = strtr($smcFunc['substr'](str_replace('<br />', "\n", $row['body']), 0, $modSettings['xmlnews_maxlen'] - 3), array("\n" => '<br />')) . '...';
  578. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  579. censorText($row['body']);
  580. censorText($row['subject']);
  581. // Being news, this actually makes sense in rss format.
  582. if ($xml_format == 'rss' || $xml_format == 'rss2')
  583. {
  584. $data[] = array(
  585. 'title' => cdata_parse($row['subject']),
  586. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  587. 'description' => cdata_parse($row['body']),
  588. 'author' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['posterEmail'] . ' ('.$row['posterName'].')' : null,
  589. 'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
  590. 'category' => '<![CDATA[' . $row['bname'] . ']]>',
  591. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['poster_time']),
  592. 'guid' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  593. );
  594. // Add the poster name on if we are rss2
  595. if ($xml_format == 'rss2')
  596. $data[sizeof($data) - 1]['dc:creator'] = $row['poster_name'];
  597. }
  598. elseif ($xml_format == 'rdf')
  599. {
  600. $data[] = array(
  601. 'title' => cdata_parse($row['subject']),
  602. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  603. 'description' => cdata_parse($row['body']),
  604. );
  605. }
  606. elseif ($xml_format == 'atom')
  607. {
  608. $data[] = array(
  609. 'title' => cdata_parse($row['subject']),
  610. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  611. 'summary' => cdata_parse($row['body']),
  612. 'category' => $row['bname'],
  613. 'author' => array(
  614. 'name' => $row['poster_name'],
  615. 'email' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['poster_email'] : null,
  616. 'uri' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  617. ),
  618. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['poster_time']),
  619. 'modified' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', empty($row['modified_time']) ? $row['poster_time'] : $row['modified_time']),
  620. 'id' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  621. );
  622. }
  623. // The biggest difference here is more information.
  624. else
  625. {
  626. $data[] = array(
  627. 'time' => htmlspecialchars(strip_tags(timeformat($row['poster_time']))),
  628. 'id' => $row['id_topic'],
  629. 'subject' => cdata_parse($row['subject']),
  630. 'body' => cdata_parse($row['body']),
  631. 'poster' => array(
  632. 'name' => cdata_parse($row['poster_name']),
  633. 'id' => $row['id_member'],
  634. 'link' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : '',
  635. ),
  636. 'topic' => $row['id_topic'],
  637. 'board' => array(
  638. 'name' => cdata_parse($row['bname']),
  639. 'id' => $row['id_board'],
  640. 'link' => $scripturl . '?board=' . $row['id_board'] . '.0',
  641. ),
  642. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  643. );
  644. }
  645. }
  646. $smcFunc['db_free_result']($request);
  647. return $data;
  648. }
  649. /**
  650. * Get the recent topics to display.
  651. * The returned array will be generated to match the xml_format.
  652. * @todo does not belong here.
  653. *
  654. * @param $xml_format
  655. * @return array, of recent posts
  656. */
  657. function action_xmlrecent($xml_format)
  658. {
  659. global $user_info, $scripturl, $modSettings, $board;
  660. global $query_this_board, $smcFunc, $settings, $context;
  661. $done = false;
  662. $loops = 0;
  663. while (!$done)
  664. {
  665. $optimize_msg = implode(' AND ', $context['optimize_msg']);
  666. $request = $smcFunc['db_query']('', '
  667. SELECT m.id_msg
  668. FROM {db_prefix}messages AS m
  669. INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
  670. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  671. WHERE ' . $query_this_board . (empty($optimize_msg) ? '' : '
  672. AND {raw:optimize_msg}') . (empty($board) ? '' : '
  673. AND m.id_board = {int:current_board}') . ($modSettings['postmod_active'] ? '
  674. AND m.approved = {int:is_approved}' : '') . '
  675. ORDER BY m.id_msg DESC
  676. LIMIT {int:limit}',
  677. array(
  678. 'limit' => $_GET['limit'],
  679. 'current_board' => $board,
  680. 'is_approved' => 1,
  681. 'optimize_msg' => $optimize_msg,
  682. )
  683. );
  684. // If we don't have $_GET['limit'] results, try again with an unoptimized version covering all rows.
  685. if ($loops < 2 && $smcFunc['db_num_rows']($request) < $_GET['limit'])
  686. {
  687. $smcFunc['db_free_result']($request);
  688. if (empty($_REQUEST['boards']) && empty($board))
  689. unset($context['optimize_msg']['lowest']);
  690. else
  691. $context['optimize_msg']['lowest'] = $loops ? 'm.id_msg >= t.id_first_msg' : 'm.id_msg >= (t.id_last_msg - t.id_first_msg) / 2';
  692. $loops++;
  693. }
  694. else
  695. $done = true;
  696. }
  697. $messages = array();
  698. while ($row = $smcFunc['db_fetch_assoc']($request))
  699. $messages[] = $row['id_msg'];
  700. $smcFunc['db_free_result']($request);
  701. if (empty($messages))
  702. return array();
  703. // Find the most recent posts this user can see.
  704. $request = $smcFunc['db_query']('', '
  705. SELECT
  706. m.smileys_enabled, m.poster_time, m.id_msg, m.subject, m.body, m.id_topic, t.id_board,
  707. b.name AS bname, t.num_replies, m.id_member, m.icon, mf.id_member AS id_first_member,
  708. IFNULL(mem.real_name, m.poster_name) AS poster_name, mf.subject AS first_subject,
  709. IFNULL(memf.real_name, mf.poster_name) AS first_poster_name, mem.hide_email,
  710. IFNULL(mem.email_address, m.poster_email) AS poster_email, m.modified_time
  711. FROM {db_prefix}messages AS m
  712. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  713. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  714. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
  715. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  716. LEFT JOIN {db_prefix}members AS memf ON (memf.id_member = mf.id_member)
  717. WHERE m.id_msg IN ({array_int:message_list})
  718. ' . (empty($board) ? '' : 'AND t.id_board = {int:current_board}') . '
  719. ORDER BY m.id_msg DESC
  720. LIMIT {int:limit}',
  721. array(
  722. 'limit' => $_GET['limit'],
  723. 'current_board' => $board,
  724. 'message_list' => $messages,
  725. )
  726. );
  727. $data = array();
  728. while ($row = $smcFunc['db_fetch_assoc']($request))
  729. {
  730. // Limit the length of the message, if the option is set.
  731. if (!empty($modSettings['xmlnews_maxlen']) && $smcFunc['strlen'](str_replace('<br />', "\n", $row['body'])) > $modSettings['xmlnews_maxlen'])
  732. $row['body'] = strtr($smcFunc['substr'](str_replace('<br />', "\n", $row['body']), 0, $modSettings['xmlnews_maxlen'] - 3), array("\n" => '<br />')) . '...';
  733. $row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $row['id_msg']);
  734. censorText($row['body']);
  735. censorText($row['subject']);
  736. // Doesn't work as well as news, but it kinda does..
  737. if ($xml_format == 'rss' || $xml_format == 'rss2')
  738. {
  739. $data[] = array(
  740. 'title' => $row['subject'],
  741. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  742. 'description' => cdata_parse($row['body']),
  743. 'author' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['poster_email'] : null,
  744. 'category' => cdata_parse($row['bname']),
  745. 'comments' => $scripturl . '?action=post;topic=' . $row['id_topic'] . '.0',
  746. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $row['poster_time']),
  747. 'guid' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg']
  748. );
  749. // add the poster name on if we are rss2
  750. if ($xml_format == 'rss2')
  751. $data[sizeof($data) - 1]['dc:creator'] = $row['poster_name'];
  752. }
  753. elseif ($xml_format == 'rdf')
  754. {
  755. $data[] = array(
  756. 'title' => $row['subject'],
  757. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  758. 'description' => cdata_parse($row['body']),
  759. );
  760. }
  761. elseif ($xml_format == 'atom')
  762. {
  763. $data[] = array(
  764. 'title' => $row['subject'],
  765. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  766. 'summary' => cdata_parse($row['body']),
  767. 'category' => $row['bname'],
  768. 'author' => array(
  769. 'name' => $row['poster_name'],
  770. 'email' => in_array(showEmailAddress(!empty($row['hide_email']), $row['id_member']), array('yes', 'yes_permission_override')) ? $row['poster_email'] : null,
  771. 'uri' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : ''
  772. ),
  773. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $row['poster_time']),
  774. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', empty($row['modified_time']) ? $row['poster_time'] : $row['modified_time']),
  775. 'id' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'],
  776. );
  777. }
  778. // A lot of information here. Should be enough to please the rss-ers.
  779. else
  780. {
  781. $data[] = array(
  782. 'time' => htmlspecialchars(strip_tags(timeformat($row['poster_time']))),
  783. 'id' => $row['id_msg'],
  784. 'subject' => cdata_parse($row['subject']),
  785. 'body' => cdata_parse($row['body']),
  786. 'starter' => array(
  787. 'name' => cdata_parse($row['first_poster_name']),
  788. 'id' => $row['id_first_member'],
  789. 'link' => !empty($row['id_first_member']) ? $scripturl . '?action=profile;u=' . $row['id_first_member'] : ''
  790. ),
  791. 'poster' => array(
  792. 'name' => cdata_parse($row['poster_name']),
  793. 'id' => $row['id_member'],
  794. 'link' => !empty($row['id_member']) ? $scripturl . '?action=profile;u=' . $row['id_member'] : ''
  795. ),
  796. 'topic' => array(
  797. 'subject' => cdata_parse($row['first_subject']),
  798. 'id' => $row['id_topic'],
  799. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.new#new'
  800. ),
  801. 'board' => array(
  802. 'name' => cdata_parse($row['bname']),
  803. 'id' => $row['id_board'],
  804. 'link' => $scripturl . '?board=' . $row['id_board'] . '.0'
  805. ),
  806. 'link' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg']
  807. );
  808. }
  809. }
  810. $smcFunc['db_free_result']($request);
  811. return $data;
  812. }
  813. /**
  814. * Get the profile information for member into an array,
  815. * which will be generated to match the xml_format.
  816. * @todo refactor.
  817. *
  818. * @param $xml_format
  819. * @return array, of profile data.
  820. */
  821. function action_xmlprofile($xml_format)
  822. {
  823. global $scripturl, $memberContext, $user_profile, $modSettings, $user_info;
  824. // You must input a valid user....
  825. if (empty($_GET['u']) || loadMemberData((int) $_GET['u']) === false)
  826. return array();
  827. // Make sure the id is a number and not "I like trying to hack the database".
  828. $_GET['u'] = (int) $_GET['u'];
  829. // Load the member's contextual information!
  830. if (!loadMemberContext($_GET['u']) || !allowedTo('profile_view_any'))
  831. return array();
  832. // Okay, I admit it, I'm lazy. Stupid $_GET['u'] is long and hard to type.
  833. $profile = &$memberContext[$_GET['u']];
  834. if ($xml_format == 'rss' || $xml_format == 'rss2')
  835. $data = array(array(
  836. 'title' => cdata_parse($profile['name']),
  837. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  838. 'description' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  839. 'comments' => $scripturl . '?action=pm;sa=send;u=' . $profile['id'],
  840. 'pubDate' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['date_registered']),
  841. 'guid' => $scripturl . '?action=profile;u=' . $profile['id'],
  842. ));
  843. elseif ($xml_format == 'rdf')
  844. $data = array(array(
  845. 'title' => cdata_parse($profile['name']),
  846. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  847. 'description' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  848. ));
  849. elseif ($xml_format == 'atom')
  850. $data[] = array(
  851. 'title' => cdata_parse($profile['name']),
  852. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  853. 'summary' => cdata_parse(isset($profile['group']) ? $profile['group'] : $profile['post_group']),
  854. 'author' => array(
  855. 'name' => $profile['real_name'],
  856. 'email' => in_array(showEmailAddress(!empty($profile['hide_email']), $profile['id']), array('yes', 'yes_permission_override')) ? $profile['email'] : null,
  857. 'uri' => !empty($profile['website']) ? $profile['website']['url'] : ''
  858. ),
  859. 'published' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $user_profile[$profile['id']]['date_registered']),
  860. 'updated' => gmstrftime('%Y-%m-%dT%H:%M:%SZ', $user_profile[$profile['id']]['last_login']),
  861. 'id' => $scripturl . '?action=profile;u=' . $profile['id'],
  862. 'logo' => !empty($profile['avatar']) ? $profile['avatar']['url'] : '',
  863. );
  864. else
  865. {
  866. $data = array(
  867. 'username' => $user_info['is_admin'] || $user_info['id'] == $profile['id'] ? cdata_parse($profile['username']) : '',
  868. 'name' => cdata_parse($profile['name']),
  869. 'link' => $scripturl . '?action=profile;u=' . $profile['id'],
  870. 'posts' => $profile['posts'],
  871. 'post-group' => cdata_parse($profile['post_group']),
  872. 'language' => cdata_parse($profile['language']),
  873. 'last-login' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['last_login']),
  874. 'registered' => gmdate('D, d M Y H:i:s \G\M\T', $user_profile[$profile['id']]['date_registered'])
  875. );
  876. // Everything below here might not be set, and thus maybe shouldn't be displayed.
  877. if ($profile['gender']['name'] != '')
  878. $data['gender'] = cdata_parse($profile['gender']['name']);
  879. if ($profile['avatar']['name'] != '')
  880. $data['avatar'] = $profile['avatar']['url'];
  881. // If they are online, show an empty tag... no reason to put anything inside it.
  882. if ($profile['online']['is_online'])
  883. $data['online'] = '';
  884. if ($profile['signature'] != '')
  885. $data['signature'] = cdata_parse($profile['signature']);
  886. if ($profile['blurb'] != '')
  887. $data['blurb'] = cdata_parse($profile['blurb']);
  888. if ($profile['location'] != '')
  889. $data['location'] = cdata_parse($profile['location']);
  890. if ($profile['title'] != '')
  891. $data['title'] = cdata_parse($profile['title']);
  892. if ($profile['website']['title'] != '')
  893. $data['website'] = array(
  894. 'title' => cdata_parse($profile['website']['title']),
  895. 'link' => $profile['website']['url']
  896. );
  897. if ($profile['group'] != '')
  898. $data['position'] = cdata_parse($profile['group']);
  899. if (!empty($modSettings['karmaMode']))
  900. $data['karma'] = array(
  901. 'good' => $profile['karma']['good'],
  902. 'bad' => $profile['karma']['bad']
  903. );
  904. if (in_array($profile['show_email'], array('yes', 'yes_permission_override')))
  905. $data['email'] = $profile['email'];
  906. if (!empty($profile['birth_date']) && substr($profile['birth_date'], 0, 4) != '0000')
  907. {
  908. list ($birth_year, $birth_month, $birth_day) = sscanf($profile['birth_date'], '%d-%d-%d');
  909. $datearray = getdate(forum_time());
  910. $data['age'] = $datearray['year'] - $birth_year - (($datearray['mon'] > $birth_month || ($datearray['mon'] == $birth_month && $datearray['mday'] >= $birth_day)) ? 0 : 1);
  911. }
  912. }
  913. // Save some memory.
  914. unset($profile, $memberContext[$_GET['u']]);
  915. return $data;
  916. }